package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.dbinfo.DbInfoCondition;
import com.bwda.dsrs.filecenter.domain.condition.dbinfohistory.DbInfoHistoryCondition;
import com.bwda.dsrs.filecenter.domain.condition.dbscanresult.DbScanResultCondition;
import com.bwda.dsrs.filecenter.domain.condition.scanresulttable.ScanResultTableCondition;
import com.bwda.dsrs.filecenter.domain.condition.tableinfohistory.TableInfoHistoryCondition;
import com.bwda.dsrs.filecenter.domain.form.dbinfohistory.DbInfoHistoryQueryForm;
import com.bwda.dsrs.filecenter.domain.vo.HomeCountVo;
import com.bwda.dsrs.filecenter.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/homecount")
@Api(value = "/b/homecount", tags = "首页/历史统计")
public class HomeCountController extends BaseController {

    @Autowired
    private DbInfoService dbInfoService;
    @Autowired
    private ScanResultTableService scanResultTableService;
    @Autowired
    private DbScanResultService dbScanResultService;

    @Autowired
    private HomeCountService homeCountService;


    @Autowired
    private DbInfoHistoryService dbInfoHistoryService;

    @Autowired
    private TableInfoHistoryService tableInfoHistoryService;
    /**
     * 根据id查询稽核管理历史任务
     *
     * @param
     * @return
     */
    @SuppressWarnings("AlibabaCollectionInitShouldAssignCapacity")
    @ApiOperation(value = "首页数据统计展示", notes = "首页数据统计展示", httpMethod = "POST")
    @RequestMapping(value = "/queryHomeCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity queryHomeCount() {
        //数据库总数
        DbInfoCondition condition = new DbInfoCondition();
        condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
        int dbCount = dbInfoService.queryCount(condition);

        //表总数
        int tableCount = scanResultTableService.queryCount(new ScanResultTableCondition());
        //识别字段总数
        int columnCount =dbScanResultService.queryCount(new DbScanResultCondition());

        //计算数据分类数据
        List<HomeCountVo> dbList =  homeCountService.selectDbTypeCount();
        //计算敏感信息
        List<HomeCountVo> leList =  homeCountService.selectLevelCount();
        //计算敏感分类信息
        List<HomeCountVo> leTypeList = homeCountService.selectLevelTypeCount();
        //计算数据库和级别
        List<HomeCountVo> dbinfoTypeList = homeCountService.selectDbInfoTypeCount();
        //敏感信息汇总
        List<HomeCountVo> leWordeList = homeCountService.selectWordIdCount();

        Map map =new HashMap();
        map.put("dbCount",dbCount);
        map.put("tableCount",tableCount);
        map.put("columnCount",columnCount);
        map.put("dbList",dbList);
        map.put("leList",leList);
        map.put("leTypeList",leTypeList);
        map.put("dbinfoTypeList",dbinfoTypeList);
        map.put("leWordeList",leWordeList);
        return getSuccessResult("操作成功",map);
    }




    /**
     * 根据id查询稽核管理历史任务
     *
     * @param
     * @return
     */
    @ApiOperation(value = "根据任务ID查询任务历史任务", notes = "根据任务ID查询任务历史任务", httpMethod = "POST")
    @RequestMapping(value = "/queryTaskHistoryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity queryTaskHistoryCount(@ApiParam(value = "", required = true) @RequestParam Long taskId,@ApiParam(value = "", required = true) @RequestParam Long historyId) {
        //历史数据库总数
        DbInfoHistoryCondition condition = new DbInfoHistoryCondition();
        condition.setTaskId(taskId.intValue());
        condition.setTaskHistoryId(historyId.intValue());
        int dbCount = dbInfoHistoryService.queryCount(condition);


        //历史表总数
        TableInfoHistoryCondition conditionTabel = new TableInfoHistoryCondition();
        conditionTabel.setTaskId(taskId.intValue());
        conditionTabel.setTaskHistoryId(historyId.intValue());
        int tableCount = tableInfoHistoryService.queryCount(conditionTabel);


        //历史敏感信息表总数总数
        /*Long taskId= taskId;
        Long taskHistoryId = 101L;*/
        int columnLeveCount =homeCountService.queryLevelCount(taskId,historyId);

        //敏感标签总数 （手机，身份证。。。）
        int columnSensLabelCount =homeCountService.querySensLabelCount(taskId,historyId);

        //计算敏感.不敏感信息
        List<HomeCountVo> leList =  homeCountService.queryLevelsCount(taskId,historyId);

        //计算敏感分类占比信息
        List<HomeCountVo> leTypeList = homeCountService.queryLevelTypeCount(taskId,historyId);


        //数据标签占比
        List<HomeCountVo> leWordeList = homeCountService.queryWordIdCount(taskId,historyId);

        //noinspection AlibabaCollectionInitShouldAssignCapacity
        Map map =new HashMap();
        map.put("dbCount",dbCount);
        map.put("tableCount",tableCount);
        map.put("columnLeveCount",columnLeveCount);
        map.put("columnSensLabelCount",columnSensLabelCount);


        map.put("leList",leList);
        map.put("leTypeList",leTypeList);
        map.put("leWordeList",leWordeList);
        return getSuccessResult("操作成功",map);
    }


    /**
     * DbInfoHistoryQueryForm转换为DbInfoHistoryCondition
     *
     * @param form
     * @return
     */
    private DbInfoHistoryCondition getConditionByQueryForm(DbInfoHistoryQueryForm form) {
        DbInfoHistoryCondition condition = CopyUtil.transfer(form, DbInfoHistoryCondition.class);
        return condition;
    }

}